#!/usr/bin/env python

# Copyright 2012 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Street Identity Scope Manager - manages scope dependencies."""

_READ = 'read'
_WRITE = 'write'

_SCOPE_PREFIX = 'https://www.googleapis.com/auth/'

# Street Identity Scope - 'verified street address'
_STREETIDENTITY = 'streetidentity'
_SCOPE_STREETIDENTITY_READ = ''.join([_SCOPE_PREFIX,_STREETIDENTITY,'.',_READ])
_SCOPE_STREETIDENTITY_WRITE = ''.join([_SCOPE_PREFIX,_STREETIDENTITY,'.',_WRITE])

# Verified Age Scope - 'verified age information'
_VERIFIEDAGE = 'verifiedage'
_SCOPE_VERIFIEDAGE_READ = ''.join([_SCOPE_PREFIX,_VERIFIEDAGE,'.',_READ])
_SCOPE_VERIFIEDAGE_WRITE = ''.join([_SCOPE_PREFIX,_VERIFIEDAGE,'.',_WRITE])

# Verified Gender Scope - 'verified gender information'
_VERIFIEDGENDER = 'verifiedgender'
_SCOPE_VERIFIEDGENDER_READ = ''.join([_SCOPE_PREFIX,_VERIFIEDGENDER,'.',_READ])
_SCOPE_VERIFIEDGENDER_WRITE = ''.join([_SCOPE_PREFIX,_VERIFIEDGENDER,'.',_WRITE])

# Graph of all dependent scopes
_STREETIDENTITY_SCOPES = {
  _SCOPE_STREETIDENTITY_WRITE : [_SCOPE_STREETIDENTITY_READ],
  _SCOPE_VERIFIEDAGE_WRITE : [_SCOPE_VERIFIEDAGE_READ],
  _SCOPE_VERIFIEDGENDER_WRITE : [_SCOPE_VERIFIEDGENDER_READ],
  _SCOPE_STREETIDENTITY_READ : [],
  _SCOPE_VERIFIEDAGE_READ : [],
  _SCOPE_VERIFIEDGENDER_READ : []
}

# Descriptions for scopes
_NAME = 'name'
_SCOPES_DESCRIPTIONS = {
  _SCOPE_STREETIDENTITY_READ : { _NAME : 'Street Address'},
  _SCOPE_VERIFIEDAGE_READ : { _NAME : 'Age'},
  _SCOPE_VERIFIEDGENDER_READ : { _NAME : 'Gender'},
  }


def get_dependent_scopes(scopes, scope_graph = _STREETIDENTITY_SCOPES):
  # TODO(mmachulak): this should be a Set
  temp_scopes = []
  for scope in scopes:
    temp = get_dependent_scopes_for_scope(scope, scope_graph)
    if temp:
      for temp_scope in temp:
        if temp_scope not in temp_scopes:
          temp_scopes.append(temp_scope)
  return temp_scopes


def get_dependent_scopes_for_scope(scope, scope_graph = _STREETIDENTITY_SCOPES):
  temp_scopes = [scope]
  temp = scope_graph.get(scope)
  if temp:
    temp_scopes.extend(temp)
  return temp_scopes


def get_scope_info(scopes, info_map = _SCOPES_DESCRIPTIONS):
  temp_info_map = {}
  for scope in scopes:
    if info_map.has_key(scope):
      temp_info_map[scope] = info_map.get(scope)
  return temp_info_map


def get_scope_param(scope, param=_NAME, default= None, info_map = _SCOPES_DESCRIPTIONS):
    if info_map.has_key(scope):
      temp_info = info_map.get(scope)
      return temp_info.get(param,default)
    return default
